<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\expbao\common\controller;

use app\common\controller\Common;
use app\expbao\model\ExUser as ExUserModel;
use app\expbao\model\ExOpt as ExOptModel;
use app\expbao\model\ExOptUser as ExOptUserModel;
use think\Hook;

/**
 * expbao前台公共控制器
 * @package app\expbao\common\controller
 */
class ExController extends Common
{
    /**
     * 初始化方法
     * @author staroot <394454244@qq.com>
     */
    protected function _initialize()
    {
        // 系统开关
        if (!config('web_site_status')) {
            $this->error('站点已经关闭，请稍后访问~');
        }
        //echo "string";
        if ($userid=$this->is_login_exp()) {
            //echo "login";
            //echo $userid;
            $this->is_active_exp($userid);
        }else{
            $this->error('没有登录', url('ucenter/login'));
        }
    }
      final protected function is_login_exp()
    {
        // 判断是否登录
        if ($eid = is_exp_sign()) {
            // 已登录  返回用户的ID
            return $eid;
        } else {
            return false;
            // 未登录 返回false
        }
    }
    final protected function is_active_exp($userid)
    {
        //$userid = $this->is_exp_login();
        if ($userid) {
            $request= \think\Request::instance();
          if($request){
            $fun=$request->controller();
            $optid=ExOptModel::getidbyfun($fun);
            $opt_array=ExOptModel::select();
            $user_opt_value=ExOptUserModel::where('uid',$userid)->value('oid');
            $user_opt_array=explode(",",$user_opt_value);
            if ($opt_array) {
                    if (!in_array($optid, $user_opt_array))
                          {
                          $this->error('您访问的模块未开通,即将跳转到开通页面',url('/expbao/ucenter/active/optid/'.$optid));
                          };
            };
           }
        }

    }
    public function active($optuserid='')
    {
        if ($optuserid === null) $this->error('缺少参数');
        //echo '</br>optuserid:'.$optuserid;
        $user_id=$this->is_exp_login();
        //echo '</br>user_id:'.$user_id;
        $optid=ExOptUserModel::getoptidbyid($optuserid);
        //echo '</br>optid:'.$optid;
        $opt=ExOptModel::getoptbyid($optid);
        //echo '</br>opt:'.$opt;
        $this->assign('user_id', $user_id);
        $this->assign('optuserid', $optuserid);
        $this->assign('optid', $optid);
        $this->assign('opt', $opt);
        return $this->fetch(); 
    }
}
